/*
 * @Author: xiaosihan
 * @Date: 2024-08-04 21:18:45
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-08-04 21:59:37
 */

// partsDetailsRender.ts
/*
 * @Author: xiaosihan
 * @Date: 2023-02-06 15:22:57
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-08-04 01:54:59
 */
import {
  AmbientLight,
  DirectionalLight,
  EquirectangularReflectionMapping,
  HemisphereLight,
  MeshPhysicalMaterial,
  Vector3,
} from "three";
import ThreeBase from "three-base";
import PointLight from "three-base/PointLight/PointLight";
import threeLoader from "three-base/threeLoader";
import threeUtils from "three-base/threeUtils";
import BlastMesh from "../home/component3d/BlastMesh/BlastMesh";
import MeshBase from "three-base/MeshBase";
import { get } from "lodash";

/**
 * @class HomeRender
 * @extends {ThreeBase}
 */
class PartsDetailsRender extends ThreeBase {
  constructor() {
    super();
  }

  // 环境光
  ambientLight = (() => {
    const ambientLight = new AmbientLight("#fff", 3);
    this.scene.add(ambientLight);
    return ambientLight;
  })();

  //点光源
  pointLight = (() => {
    const pointLight = new PointLight("#fff", 0.5);
    pointLight.distance = 1000;
    pointLight.decay = 0;
    pointLight.position.set(50, 40, 50);
    this.scene.add(pointLight);
    return pointLight;
  })();

  //半球光
  hemisphereLight = (() => {
    const hemisphereLight = new HemisphereLight("#c7f8ff", "#000000");
    this.scene.add(hemisphereLight);
    return hemisphereLight;
  })();

  // 方向光2
  directionalLight2 = (() => {
    const directionalLight = new DirectionalLight("#fff", 0.5);
    directionalLight.position.set(10, 10, 0);
    directionalLight.target.position.set(0, 0, 0);
    directionalLight.castShadow = true;
    directionalLight.shadow.mapSize.set(1024, 1024);
    this.scene.add(directionalLight);
    return directionalLight;
  })();

  // 方向光
  directionalLight = (() => {
    const directionalLight = new DirectionalLight("#fff", 0.5);
    directionalLight.position.set(10, 10, 5);
    directionalLight.target.position.set(0, 0, 0);
    directionalLight.castShadow = true;
    directionalLight.shadow.mapSize.set(1024, 1024);
    this.scene.add(directionalLight);
    return directionalLight;
  })();

  init() {
    super.init();
    this.axesHelper.visible = false;
    this.gridHelper.visible = false;
    this.controls.enableZoom = false;
    this.controls.enablePan = false;
    this.controls.enableDamping = false;
    this.autoRender = false;

    // const envMap = threeLoader.getTexture(blueskyJPG).clone();
    // envMap.mapping = EquirectangularReflectionMapping;
    // this.scene.environment = envMap;

    this.resetLookAt({
      center: new Vector3(0, 0, 0),
      position: new Vector3(5.781, 2.037, 5.14),
    });
  }

  async loadModel(url: string) {
    this.needRender = true;
    const extension = get(url.match(/\.\w+(?=(\?|$))/), [0]);
    await this.mesh.loadModel(url, `.${extension}`);
  }

  //爆炸模型
  mesh = (() => {
    const mesh = new MeshBase();
    mesh.addEventListener("loaded", () => {
      threeUtils.center(mesh);
      this.lookatObjects({
        objects: [mesh],
        direction: new Vector3(-1, 1, 1),
        duration: 0,
      });
    });
    this.scene.add(mesh);
    return mesh;
  })();

  render() {
    super.render();
  }
}

const partsDetailsRender = (window.partsDetailsRender =
  new PartsDetailsRender());

export default partsDetailsRender;
